﻿using ProjectCloth.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class ProductSelectFrm : Form
    {
        private ClothContext db = new ClothContext();
        List<Product> List = new List<Product>();
        public string productNo;
        public ProductSelectFrm()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            List = db.Products.Where(o => o.Enabled == true).ToList();
        }

        private void searchBth_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = List.Where(o => o.ProductNo.Contains(productNoTxt.Text)).ToList();
        }

        private void productNoTxt_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = List.Where(o => o.ProductNo.Contains(productNoTxt.Text)).ToList();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (dataGridView1.CurrentRow.Cells["ProductNo"].Value != null)
                {
                    productNo = dataGridView1.CurrentRow.Cells["ProductNo"].Value.ToString();
                    this.Close();
                }

            }
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            //{
            //    if (dataGridView1.CurrentRow.Cells["ProductNo"].Value != null)
            //    {
            //        productNo = dataGridView1.CurrentRow.Cells["ProductNo"].Value.ToString();
            //        this.Close();
            //    }

            //}
        }
        //改变键盘键enter效果
        #region
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {

            base.ProcessCmdKey(ref msg, keyData);
            var dgv = dataGridView1;
            switch (keyData)
            {
                case Keys.Enter:
                    if (dgv.CurrentRow != null)
                    {
                        if (dataGridView1.CurrentRow.Cells["ProductNo"].Value != null)
                        {
                            productNo = dataGridView1.CurrentRow.Cells["ProductNo"].Value.ToString();
                            this.Close();
                        }
                        //if (dgv.CurrentRow.Cells["ProductNo"] != null && dgv.CurrentRow.Cells["ProductNo"].Value != null)
                        //{

                        //    var productno = dgv.CurrentRow.Cells["ProductNo"].Value.ToString();
                        //    //var product = productList.Where(p => p.ProductNo.Contains(productno)).FirstOrDefault();// productlist.Where(p => p.ProductId == productno).FirstOrDefault();
                        //    //if (product != null)//&& sel)
                        //    {
                        //    }
                        //}
                    }
                    break;

            }
            return false;
        }
        #endregion
    }
}
